package cn.ylyhappy.ideavimbuilder4;


import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.core.content.FileProvider;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import cn.ylyhappy.ideavimbuilder4.constant.Constant;
import cn.ylyhappy.ideavimbuilder4.db.DbHelper;
import cn.ylyhappy.ideavimbuilder4.model.IdeaVimConfig;

public class ConfigPreviewActivity extends BaseActivity {
    private TextView tv1;
    private TextView tv2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_preview_config);
        initInsets();

        Intent intent = getIntent();
        // 这个api需要min33 intent.getParcelableExtra("ConfigData", IdeaVimConfig.class);
        IdeaVimConfig configData = intent.getParcelableExtra(Constant.INTENT_IDEA_VIM_CONFIG_DATA);

        // title text
        tv1 = findViewById(R.id.activity_preview_textview1);
        // preview text
        tv2 = findViewById(R.id.activity_preview_textview2);

        tv1.setText(configData.getName());
        String preivew = DbHelper.getInstance().getPreviewIdeaVimConfig(configData.getId(), false);
        tv2.setText(preivew);

        findViewById(R.id.activity_preview_button).setOnClickListener(view -> {
            try {
                File file = writeTextFile(preivew);
//                sendFileViaEmail(this,file);
                shareFileViaIntent(file);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });

    }
    public File writeTextFile(String str) throws IOException {
        String filePath = this.getFilesDir().getPath() + "/ideavimrc";
        File file = new File(filePath);
        try(FileOutputStream fis = new FileOutputStream(file)) {
            fis.write(str.getBytes(StandardCharsets.UTF_8));
        }
        return file;
    }

    public void sendFileViaEmail(Context context, File file) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("message/rfc822"); // 邮件类型

        // 设置收件人邮箱（可选）
        intent.putExtra(Intent.EXTRA_EMAIL, new String[]{"2778335106@qq.com"});

        // 设置邮件主题
        intent.putExtra(Intent.EXTRA_SUBJECT, "文件附件");

        // 设置邮件正文
        intent.putExtra(Intent.EXTRA_TEXT, "请查收附件");

        // 添加附件
        Uri fileUri = FileProvider.getUriForFile(
                context,
                context.getApplicationContext().getPackageName() + ".provider",
                file
        );
        intent.putExtra(Intent.EXTRA_STREAM, fileUri);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

        // 启动邮件客户端选择器
        context.startActivity(Intent.createChooser(intent, "选择邮件客户端"));
    }

    public void shareFileViaIntent(File file) {
        // 获取文件的 content:// URI
        Uri fileUri = FileProvider.getUriForFile(
                this,
                getPackageName() + ".provider",
                file
        );

        // 创建分享 Intent
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain"); // 设置文件类型
        shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // 授予临时读取权限

        // 检查是否有应用可以处理该 Intent
        if (shareIntent.resolveActivity(getPackageManager()) != null) {
            startActivity(Intent.createChooser(shareIntent, "分享文件"));
        } else {
            Toast.makeText(this, "没有可用的分享应用", Toast.LENGTH_SHORT).show();
        }
    }
}
